package com.briup.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.briup.common.bean.TblCategory;
import com.briup.common.bean.TblLink;
import com.briup.common.util.MyBatisSqlSessionFactory;
import com.briup.service.impl.TblCategoryServiceImpl;
import com.briup.service.impl.TblLinkServiceImpl;

/**
 * Servlet implementation class to_index_Servlet
 */
@WebServlet("/jsp/to_index_Servlet")
public class to_index_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public to_index_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SqlSession openSession = MyBatisSqlSessionFactory.openSession() ;
		List<TblCategory> categories = new ArrayList<TblCategory>() ;
		try {
			TblCategoryServiceImpl tblCategoryServiceImpl = new TblCategoryServiceImpl() ;
			categories = tblCategoryServiceImpl.selectCategorys(openSession) ;
			request.getSession().setAttribute("categories", categories);
			TblLinkServiceImpl tblLinkServiceImpl = new TblLinkServiceImpl() ;
			List<TblLink> links = tblLinkServiceImpl.selectTblLink(openSession) ;
			request.getSession().setAttribute("links", links);
		} catch (Exception e) {
			System.out.println(e.getCause().toString());
			e.printStackTrace();
		}finally {
			openSession.close();
			response.sendRedirect("index.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
